package com.xiaoxu.privatechat.controller.admin;

import com.xiaoxu.privatechat.bean.PageBean;
import com.xiaoxu.privatechat.bean.Result;
import com.xiaoxu.privatechat.entity.admin.DatabaseBak;
import com.xiaoxu.privatechat.service.admin.DatabaseBakService;
import com.xiaoxu.privatechat.service.admin.OperaterLogService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.File;

/**
 * @author xiaoxu
 * @description 数据库备份管理控制器
 * @date 2023/2/13
 */
@RequestMapping("/admin/database_bak")
@Controller
public class DatabaseBakController {

    @Resource
    private OperaterLogService operaterLogService;

    @Resource
    private DatabaseBakService databaseBakService;

    /**
     * 备份地址
     */
    @Value("${ylrc.database.backup.dir}")
    private String backUpDir;

    private final Logger log = LoggerFactory.getLogger(DatabaseBakController.class);

    /**
     * @description 数据库备份文件管理列表页面
     * @param model freemarker模型
     * @param pageBean 分页
     * @return String
     */
    @RequestMapping(value = "/list")
    public String list(Model model, PageBean<DatabaseBak> pageBean) {
        model.addAttribute("title", "備份列表");
        model.addAttribute("pageBean", databaseBakService.findList(pageBean));
        return "admin/database_bak/list";
    }

    /**
     * @description 数据库备份操作
     * @return Result
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> add() {
        databaseBakService.backup();
        return Result.success(true);
    }

    /**
     * @description 删除备份的记录及文件
     * @param ids 文件id集
     * @return Result
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> delete(String ids) {
        if (!StringUtils.isEmpty(ids)) {
            String[] splitIds = ids.split(",");
            for (String id : splitIds) {
                DatabaseBak databaseBak = databaseBakService.find(Long.valueOf(id));
                if (databaseBak != null) {
                    databaseBakService.delete(Long.valueOf(id));
                    File file = new File(databaseBak.getFilepath() + databaseBak.getFilename());
                    if (!file.exists()) {
                        //此时说明文件不存在，按照配置文件的路径重新寻找文件
                        file = new File(backUpDir + databaseBak.getFilename());
                    }
                    file.delete();
                    log.info("删除資料庫備份檔案，備份ID=" + id);
                }
            }
        }
        return Result.success(true);
    }

    /**
     * @description 还原数据库文件
     * @param id 备份id
     * @return Result
     */
    @RequestMapping(value = "restore", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> restore(@RequestParam(name = "id", required = true) Long id) {
        databaseBakService.restore(id);
        return Result.success(true);
    }
}
